package com.leigq.basaltools.util;

import com.leigq.basaltools.enumeration.GlobalEnums;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户设备工具类
 * @author leigq
 */
public abstract class DeviceUtils {

    /**
     * 获取用户设备平台
     * <br/>
     * Android、ISO、Web
     *
     * @param request the request
     * @return Android、ISO、Web
     */
    public static String devicePlatform(HttpServletRequest request) {
        String userAgent = request.getHeader(GlobalEnums.RequestHeader.USER_AGENT.toString());

        if (userAgent == null) {
            throw new NullPointerException("userAgent is null");
        }

        if ("".equals(userAgent.trim())) {
            throw new NullPointerException("userAgent is null");
        }

        if (userAgent.contains(GlobalEnums.DevicePlatform.ANDROID.toString())) {
            return GlobalEnums.DevicePlatform.ANDROID.toString();
        }

        if (userAgent.contains("Safari")) {
            return GlobalEnums.DevicePlatform.IOS.toString();
        }

        return GlobalEnums.DevicePlatform.WEB.toString();
	}
}
